/**
 * @create 2022-04-17
 * @desc pro-table
 */

import { DataContextProvider } from './context/data';
import type { ProTableProps } from './types/pro-table';
import { ProTableStoreProvider } from './context/store';
import { TableContainer } from './components/table-container';

export const ProTable = <T extends Record<string, any>, Q extends Record<ObjectKey, any> = any>(
  props: ProTableProps<T, Q>,
) => {
  const { params: propsParams, fetcher, table, rowKey, ...restProps } = props;

  const newTableProps = {
    ...table,
    rowKey,
  };

  const PageComp = (
    <ProTableStoreProvider pagination={newTableProps.pagination} params={propsParams}>
      <DataContextProvider fetcher={fetcher}>
        <TableContainer<T> table={newTableProps} {...restProps} />
      </DataContextProvider>
    </ProTableStoreProvider>
  );

  return PageComp;
};
